/**
 * Created by ali on 2017/12/4.
 */
var datalist = {
    "authority": [
        {
            "id": 1001,
            "name": "普通用户权限",
            "state": '正常',
            "creatTime": "2017-10-09",
            "lastModifiedTime": "2017-11-20"
        },
        {
            "id": 1002,
            "name": "一级分销商",
            "state": '正常',
            "creatTime": "2017-10-09",
            "lastModifiedTime": "2017-11-20"
        },
        {
            "id": 1003,
            "name": "二级分销商",
            "state": '正常',
            "creatTime": "2017-10-09",
            "lastModifiedTime": "2017-11-20"
        },
        {
            "id": 1004,
            "name": "系统管理员",
            "state": '正常',
            "creatTime": "2017-10-09",
            "lastModifiedTime": "2017-11-20"
        }
    ]
};

var flag; //设置添加或修改的标识
var attrId = []; //存页面的所有id
$(function() {

    loadData(datalist.authority);
});

//加载数据
function loadData(data) {
    $('#tabelData').datagrid('loadData', data);
    $('#tabelData').datagrid({
        //设置只能选中单行
        onClickRow: function(rowIndex, rowData) {
            $('#tabelData').datagrid('unselectAll');
            $('#tabelData').datagrid('selectRow', rowIndex);
        },
        //双击事件
        onDblClickRow: function(rowIndex, rowData) {
            $('#tabelData').datagrid('unselectAll');
            $('#tabelData').datagrid('selectRow', rowIndex);
            detail(rowIndex);
        }
    });
}

//得到页面所有数据的id
function getRowsId() {
    var dataRows = $('#tabelData').datagrid('getRows');
    for(var i = 0; i < dataRows.length; i++) {
        attrId.push(dataRows[i].id);
    }
    return attrId;
}

//查询
function searchs() {


}

//添加
function add() {
    flag = true;
    $("#dlg").dialog({ title: "新增角色" });
    $("#id").textbox({ disabled: false });
    $("#dlg").dialog("open");
    $("#id").textbox().next("span").find('input').focus();
}

//修改
function modify() {
    var seletRows = $('#tabelData').datagrid('getSelections');
    $("#id").textbox({ disabled: true });
    $("#name").textbox().next("span").find('input').focus();

    if(seletRows.length == 0) {
        alert("未选中数据");
    } else if(seletRows.length > 1) {
        alert("不能选择多条数据");
    } else {
        flag = false;
        $("#dlg").dialog({ title: "修改角色" });
        $("#dlg").dialog("open");
        $("#fr").form("load", seletRows[0]);
    }
}

//保存
function save() {
    getRowsId();
    //从本地得到数据
    var list = getLocalStorage();
    //创建一个新的json对象
    var newUser = {};

    newUser.id = $("#id").val();
    newUser.name = $("#name").val();
    newUser.gender = $("#gender").val();
    newUser.eMail = $("#eMail").val();
    newUser.phone = $("#phone").val();
    newUser.address = $("#address").val();
    newUser.employeeType = $("#employeeType").val();
    newUser.job = $("#job").val();

    if($("#fr").form("validate")) {
        //判断是添加还是修改
        if(flag) {
            setLocalStorage(newUser);
        } else {
            $.each(list, function(i) {
                if(list[i].id == $("#id").val()) {
                    list.splice(i, 1, newUser);
                }
            });
            setLocalStorage(list);
        }
        //关闭模态框
        $("#dlg").dialog("close");
        //清空表单中的数据
        $("#fr").form("reset");
    }

    //加载数据
    $('#tabelData').datagrid('loadData', getLocalStorage());
}

//取消
function cancel() {
    $("#dlg").dialog("close");
    $("#detail_dlg").dialog("close");
    $("#fr").form("reset");
}

//删除
function remove() {
   
}

//在表格中添加 详情
function opration(value, row, index) {
    return "<a href='#' onclick='detail(" + index + ")'>详情</a>";
}

// 详情
function detail(index) {

}
